package cn.ztydata.teachingsystem.heaven.service;

import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.dao.CourseCodeDao;
import cn.ztydata.teachingsystem.heaven.entity.CourseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程代码服务类
 * Created by chenxin on 2014/12/9.
 */
@Service
public class CourseCodeService extends BaseService {
    @Autowired
    private CourseCodeDao courseCodeDao;

    @Autowired
    private TeacherTeamService teacherTeamService;

    @Autowired
    private WebContext webContext;

    /**
     * 新增课程教务代码
     * @param courseCode 教务代码
     *
     * @author cx
     * @since 2014-12-09
     */
    public void save(CourseCode courseCode) {
        //判断用户是否在课程的教师团队
        teacherTeamService.isMemeber(courseCode.getCourseId());

        //如果新增的课程代码已存在
        if (courseCodeDao.isExist(courseCode)) {
            log.debug("添加的课程教务代码已经存在:courseCodeDao.isExist(courseCode)");
            throw new ServiceException("添加的课程教务代码已经存在");
        }

        //保存新增的课程代码
        if (!courseCodeDao.save(courseCode)) {
            log.debug("保存新增的课程代码失败：!courseCodeDao.save(courseCode)");
            throw new ServiceException();
        }
    }

    /**
     * 编辑课程教务代码
     * @param courseCode 教务代码
     *
     * @author cx
     * @since 2014-12-09
     */
    public void update(CourseCode courseCode) {
        //判断用户是否在课程的教师团队
        teacherTeamService.isMemeber(courseCode.getCourseId());

        //编辑保存课程代码
        if (!courseCodeDao.update(courseCode)) {
            log.debug("编辑保存课程代码失败：!courseCodeDao.update(courseCode)");
            throw new ServiceException();
        }
    }

    /**
     * 删除课程教务代码
     * @param id 教务代码ID
     *
     * @author cx
     * @since 2014-12-09
     */
    public void delete(final long id, final long courseId) {
        //判断用户是否在课程的教师团队
        teacherTeamService.isMemeber(courseId);

        //删除课程教务代码
        if (!courseCodeDao.delete(id)) {
            log.debug("删除课程教务代码失败：!courseCodeDao.delete(id)");
            throw new ServiceException();
        }
    }

    /**
     * 获取指定课程的教务代码
     * @param courseId 课程代码
     * @return List
     *
     * @author cx
     * @since 2014-12-09
     */
    public List<CourseCode> getAll(final long courseId) {
        log.debug("获取指定课程的教务代码");
        return courseCodeDao.getAll(courseId);
    }
}
